/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

package common;

import java.util.*;
import java.lang.*;

/**
 *вспомогательный класс содержащий разные полезняшки
 */
public class Helper
{
    public static void printSet(Set<String> pset, String setName)
    {
	String outstr = ":<";

	for(String item : pset)
	    {
		outstr += item+",";
	    }

	outstr = outstr.substring(0, outstr.length() - 1);
	System.out.println(setName+outstr+">");
    }

    public static void printStrArray(String[] arr)
    {
	for(int i = 0; i < arr.length; i++)
	    {
		System.out.println(""+i+":'"+arr[i]+"'");
	    }
    }
    
    public static StackTraceElement getStackTracePoint()
    {
    	Throwable tr = new Throwable();
	tr.fillInStackTrace();
	return tr.getStackTrace()[1];
    }

    public static void printStackTracePoint(String suffix)
    {
    	Throwable tr = new Throwable();
	tr.fillInStackTrace();
	System.out.println("[DEBUG] "+tr.getStackTrace()[1].toString()+suffix);
    }

    public static void logStackTrace(Exception ex)
    {
	StackTraceElement[] elements = ex.getStackTrace();
	System.out.println(ex.toString());

	for(int i = 0; i < elements.length; i++)
	    {
		System.out.println(elements[i].toString());
	    }
	elements = null;
    }

    public static String stringJoin(Vector<String> items, String delim)
    {
	String result = "";

	for(String item : items)
	    {
		result += item+delim;
	    }
	
	if(result.length() > delim.length())
	    {
		return result.substring(0, result.length() - delim.length());
	    }

 	return result;
    }

    public static String stringJoin(String[] items, String delim)
    {
	String result = "";

	for(String item : items)
	    {
		result += item+delim;
	    }
	
	if(result.length() > delim.length())
	    {
		return result.substring(0, result.length() - delim.length());
	    }

 	return result;
    }

    public static String stringJoin(Set<String> items, String delim)
    {
	String result = "";

	for(String item : items)
	    {
		result += item+delim;
	    }
	
	if(result.length() > delim.length())
	    {
		return result.substring(0, result.length() - delim.length());
	    }

	return result;
    }

    public static String stringJoin(TreeMap<String, String> items, String delim)
    {
	String result = "";

	for(String key : items.keySet())
	    {
		result += key+"="+items.get(key)+delim;
	    }

	if(result.length() > delim.length())
	    {
		return result.substring(0, result.length() - delim.length());
	    }

	return result;
    }
    
    public static void sleep(long millis)
    {
	try
	    {
		Thread.sleep(millis);
	    }
	catch (InterruptedException e)
	    {
		//
	    }
    }

    // public static void introspectObject(Object obj)
    // {
    // }
}
